<?php
namespace User\Controller;
use Think\Controller;
/**
 * 子集详细列表页控制器 ListController.class.php
 */
class ListController extends CommonController{
    /* 属性声明 */
    //主配置文件
    private $main_conf_file = null;
    //扩展配置文件
    private $kz_conf_file = "./Org/Data/kz_conf.json";
    //时间验证字段配置文件
    private $sj_conf_file = "./Org/Data/sj_zd_conf.json";
    
    /* 方法声明 */
    /**
     * 构造方法
     */
    public function __construct() {
        parent::__construct();
        //检测用户合法性
        if(!$this -> uuid = session('uuid')){
            $this -> error('获取员工标识失败!');
        }
        $data = I('get.');

        //获取菜单项
        if(!$this -> menu = $data['menu']){
            $this -> error('非法参数传递!');
        }
        $this -> corpid = session('corpid');

        $this -> main_conf_file = "./Org/Data/UserData/main_conf/{$this->corpid}_main.conf";
        //初始化前台js字典文件
        $this -> dic_file = './Org/Data/Dic_js/'.$this->corpid.'_dic.js';
    }
    
    /**
     * 入口方法
     */
    public function index(){
        //1.获取常规字典数据
        A('Pc/Common') -> createDic();
        //2.获取已经提交审核的子集项
        $this -> redis -> select(11);
        if($change_item = $this->redis->hvals('items_'.$this->uuid)){
            $str = '';
            foreach($change_item as $k=>$v){
                $v = unserialize($v);
                $str .= implode(',',$v).',';
            }
            $change_item = rtrim($str,',');
        }else{
            $change_item = '';
        }

        //3.成员数据
        $userinfo = A('Pc/Common') -> getUserInfo($this -> uuid);

        //4.获取子集数据
        $dat = A('Pc/Common') -> getSublist($userinfo,$this->menu);
        if(!$dat){
            $this -> error('未知menu数据!');
            exit;
        }
        //dump($dat);exit;
        if($this->menu == 'BO_CASC_HR_YGRS'){
            $arr['ZP'] = $dat['sublist']['ZP'];
            foreach($dat['sublist'] as $k=>$v){
                unset($dat['sublist']['ZP']);
                $arr[$k] = $v;
            }
            $dat['sublist'] = $arr;
        }
        //dump($dat);exit;

        //5.是否可拓展标记 单值 多值
        $is_extension = $this -> menu == 'BO_CASC_HR_YGRS' ? 0 : 1;

        //6.获取扩展配置文件
        $kz_info = json_decode(file_get_contents($this->kz_conf_file),true);
        $kz_info = $kz_info[$this->menu];
        //dump($kz_info);exit;

        //7.获取时间验证字段
        $sj_info = json_decode(file_get_contents($this->sj_conf_file),true);
        $sj_info = $sj_info[$this->menu];
        //dump($sj_info);exit;

        //8.获取type为file字段
        $file_zd = [];
        foreach($dat['sublist'] as $k=>$v){
            if($v['type'] =='FILE'){
                $file_zd[] = $k;
            }
        }
        //9 .获取附件信息
        if($file_zd){
            $dat = A('Pc/Common')->getFileInfo($this->uuid,$this->menu,$file_zd,$dat);
        }
        //dump($dat);exit;
        //获取图片的URL
        if($this->menu == 'BO_CASC_HR_YGRS'){
            $zp_key = array_keys($dat['sublist']['ZP']['value']);
            $zp_img = $dat['sublist']['ZP']['value'][$zp_key[0]]['href'];
            $this->assign('zp_img',$zp_img);
        }

        $this -> assign([
            'uuid'         => $this->uuid,
            'is_extension' => $is_extension,
            'menu'         => $this->menu,
            'caption'      => $dat['caption'],
            'info'         => json_encode($dat),
            'kz_info'      => json_encode($kz_info),
            'dic'          => $this -> dic,
            'layer'        => $this -> layer,
            'change_item'  => $change_item,
            'sj_info'      => json_encode($sj_info),
        ]);
        $this -> display('list');
    }
    
    /**
     * 生成字典数据
     */
    public function createDic(){
        $f1 = file_exists($this -> dic_file);

        if(!$f1 ){
            //获取通用字典
            $dic = $this -> dic();
            //获取省份字典数据

            //生成前台js json文件 降低前台渲染数据压力
            $dic_file_data = 'var dic=' . $dic[1];
            $dic_file = './Org/Data/Dic_js/'.$this->corpid.'_dic.js';
            //检测通用字典js文件是否存在
            file_put_contents($dic_file, $dic_file_data);

        }
        $this -> dic = '/Org/Data/Dic_js/'.$this->corpid.'_dic.js';
    }
    
    /**
     * 截取caption长度为8个汉字长度
     */
    private function cut_caption($dat){
        $list = $dat['sublist'];
        //过滤结构
        foreach($list as $k => $v){
           $list[$k]['caption'] = mb_substr($v['caption'],0,7,'utf-8');
        }
        $dat['sublist'] = $list;
        //过滤值结构
        $value_list = $dat['subvalue'];
        foreach($value_list as $k => $v){
            $info = $v['info'];
            foreach($info as $k2 => $v2){
                $info[$k2]['caption'] = mb_substr($v2['caption'],0,7,'utf-8');
            }
            $value_list[$k]['info'] = $info;
        }
        $dat['subvalue'] = $value_list;
        return $dat;
    }
    
    /**
     * 获取员工数据
     * @param string $uuid 员工唯一标识
     * @return array 员工全集数据
     */
    public function getUserInfo($uuid){
        $this -> redis -> select(10);
        if(!$data = $this -> redis -> hget('user_list',$uuid)){
            $this -> error('未匹配到您的相关信息!');
        }
        return json_decode($data,true)['information'];
    }
    
    /**
     * 获取指定子集数据
     * @param array $userinfo 成员数据
     * @param string $menu 子集名称
     * @return array 返回指定菜单过滤后子集数据
     */
    private function getSublist($userinfo,$menu){
        $item = $userinfo[$menu];
        //dump($userinfo);exit;
        //检测是否存在主配置文件
        if(file_exists($this->main_conf_file)){
            $conf_info = unserialize(file_get_contents($this->main_conf_file));
            $conf = $conf_info[$menu];
            //dump($conf);

            //0.过滤标准
            $filterlist = $conf['sublist'];
            //dump($filterlist);exit;

            //1.过滤sublist
            $sublist = $item['sublist'];
            $sublist_keys = array_keys($sublist);
            //dump($sublist);exit;
            $sublist_buf = [];
            //过滤子集显示项 + 排序操作
            foreach($filterlist as $k => $v){
                if(in_array($v,$sublist_keys)){
                    $sublist_buf[$v] = $sublist[$v];
                }
            }
            //dump($sublist_buf);exit;
            //2.过滤subvalue
            $subvalue = $item['subvalue'];
            foreach($subvalue as $k => $v){
                $info = $v['info'];
                $info_keys = array_keys($info);
                //定义临时数据缓冲区
                $subvalue_buf = [];
                foreach($filterlist as $k2 => $v2){
                    if(in_array($v2,$info_keys)){
                        $subvalue_buf[$v2] = $info[$v2];
                    }
                }
                $subvalue[$k]['info'] = $subvalue_buf;
            }
            //dump($subvalue);exit;
            //3.数据重新组合
            $item['sublist'] = $sublist_buf;
            $item['subvalue'] = $subvalue;
        }
        //dump($item);exit;
        //返回最终数据
        return $item;
    }
    
    /**
     * 生成字典数据
     */
    public function dic(){
        $this -> user_dic_file = './Org/Data/UserData/dic_conf/'.$this->corpid.'_dic_conf.conf';
        //1.检测是否存在用户自定义字典配置
        if(file_exists($this->user_dic_file)){
            $data = json_decode(file_get_contents($this->user_dic_file),true);
            return [1,file_get_contents($this->user_dic_file)];
        }else{
        //2.返回默认字典数据
           $data = json_decode(file_get_contents('./Org/Data/init_dic.json'),true)['CASC.HR.DICT.ALL'];
           return [0,json_encode($data)];
        }
    }
    
    /**
     * 获取省份字典
     */
    public function pro_dic(){
        $this -> user_pro_file = './Org/Data/UserData/province/'.$this->corpid.'_pro_conf.conf';
        //检测是否存在省份字典配置文件
        if(file_exists($this -> user_pro_file)){
            return file_get_contents($this -> user_pro_file);
        }
        //返回默认省份字典文件
        return file_get_contents('./Org/Data/Original_Province.json');
    }
}